home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 14 / CU Amiga Magazine's Super CD-ROM 14 (1997)(EMAP Images)(GB)(Track 1 of 3)[!][issue 1997-09].iso / CUCD / Programming / AMOS / AMOSList0597 / AMOSLIST / text0267.txt < prev    next >
Encoding:
Text File  |  1997-06-01  |  1.6 KB  |  46 lines

  1. On 23-mei-97 Joona I Palaste wrote:
  2. >I am thinking of programming an AMOS game with robots that you can
  3. >actually program in a BASIC-type language. Unfortunately I don't know
  4. >how to write a decent arithmetic evaluation routine in AMOS. It should
  5. >evaluate expressions in the correct order, e.g. "2+(3+4)*5" is 37. I've
  6. >only managed to write an evaluation routine which doesn't care about
  7. >calculation orders, e.g. "2+3*4" would be 20, not 14. I need your help
  8. >here! Write me a routine that reads an expression as a string variable
  9. >and stores the result in a number variable. If you do this I will
  10. >eventually include your name in the game's documentation. Thanks in
  11. >advance.
  12.  
  13. Well, the way you do it now is easier to program and possibly faster than the
  14. way you want it, so why don't stick with it?
  15.  
  16. But that wasn't what you asked.
  17.  
  18. Why don't you use recursion? (Asuming that nobody uses more than, say, ten
  19. levels of brackets).
  20.  
  21. So any time you encounter an open bracket, you call the same routine with the
  22. tail of the string, and a close bracket will return to the calling routine.
  23.  
  24. Say:
  25. Procedure KALK(EXPR$,PTR)
  26.   A$=Mid$(EXPR$,PTR,1)
  27.   If A$="("
  28.     KALK(EXPR$,PTR+1)
  29.     RESULT=RESULT+Param
  30.   End If
  31.   If A$<>")"
  32.     'Do your evaluation here'
  33.   End If
  34. End Proc[RESULT]
  35.  
  36. Of course, this only solves one part of your problem. The rest could be solved
  37. by
  38. putting brackets around the expressions that need to be evaluated first.
  39.  
  40. -- 
  41. Branko Collin                                              . |. .
  42. collin@xs4all.nl                                         . . || ...
  43. http://www.xs4all.nl/~collin                          . ....||| .. ..
  44.  
  45.  
  46.